package com.xdtech.cc.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.cc.dao.TradeRecordDao;
import com.xdtech.cc.model.Account;
import com.xdtech.cc.model.TradeRecord;
import com.xdtech.cc.service.AccountService;
import com.xdtech.cc.service.TradeRecordService;
import com.xdtech.cc.vo.TradeRecordItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2016-05-29 21:53:36
 * @since 1.0
 * @see
 */
@Service
public class TradeRecordServiceImpl implements TradeRecordService {
	private Log log = LogFactory.getLog(TradeRecordServiceImpl.class);
	@Autowired
	private TradeRecordDao tradeRecordDao;
	@Autowired
	private BaseDao baseDao;
	@Autowired
	private AccountService accountService;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:36
	 * @modified by
	 * @param entity
	 */
	public void save(TradeRecord entity) {
		tradeRecordDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:36
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<TradeRecord> entities) {
		tradeRecordDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:36
	 * @modified by
	 * @param entity
	 */
	public void delete(TradeRecord entity) {
		tradeRecordDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:36
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		tradeRecordDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:36
	 * @modified by
	 * @param id
	 * @return
	 */
	public TradeRecord get(Long id) {
		return tradeRecordDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:36
	 * @modified by
	 * @return
	 */
	public List<TradeRecord> getAll() {
		return tradeRecordDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:36
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> tradeRecords = null;
		long rows = 0;
		if (pg!=null) {
			Page<TradeRecord> page = tradeRecordDao.findPage(pg,"from TradeRecord order by createTime desc", values);
			tradeRecords = BeanUtils.copyListProperties(
					TradeRecordItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<TradeRecord> tradeRecordList = tradeRecordDao.find("from TradeRecord order by id desc", values);
			tradeRecords = BeanUtils.copyListProperties(
					TradeRecordItem.class, tradeRecordList);
			rows = tradeRecords.size();
		}
		results.put("total", rows);
		results.put("rows", tradeRecords);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:36
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateTradeRecord(TradeRecordItem item) {
		TradeRecord tradeRecord = null;
		if (item.getId()==null) {
			tradeRecord = new TradeRecord();
		}else {
			tradeRecord = tradeRecordDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(tradeRecord, item);
		Account account = accountService.get(item.getAccountId());
		tradeRecord.setAccount(account);
		tradeRecordDao.save(tradeRecord);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:36
	 * @modified by
	 * @param tradeRecordId
	 * @return
	 */
	public TradeRecordItem loadTradeRecordItem(Long tradeRecordId) {
		TradeRecord tradeRecord = tradeRecordDao.get(tradeRecordId);
		return TradeRecordItem.createItem(tradeRecord);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:36
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteTradeRecordInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:36
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteTradeRecordInfo(List<Long> tradeRecordIds) {
		for (Long id : tradeRecordIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2016-05-29 21:53:36
	 * @modified by
	 * @return
	 */
	@Override
	public List<TradeRecordItem> loadItems() {
		List<TradeRecord> tradeRecords = getAll();
		return TradeRecordItem.createItems(tradeRecords);
	}

}
